package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.mapper.WarningRecordMapper;
import com.j283_5.wisdom_park.pojo.Device;
import com.j283_5.wisdom_park.pojo.Warning;
import com.j283_5.wisdom_park.pojo.WarningDelivery;
import com.j283_5.wisdom_park.pojo.WarningRecord;
import com.j283_5.wisdom_park.service.WarningRecordService;
import com.j283_5.wisdom_park.vo.device_management.DeviceInformationVo;
import com.j283_5.wisdom_park.vo.device_management.DeviceVo;
import com.j283_5.wisdom_park.vo.warning_management.WarningDeliveryVo;
import com.j283_5.wisdom_park.vo.warning_management.WarningVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
@Transactional
public class WarningRecordServiceImpl extends ServiceImpl<WarningRecordMapper, WarningRecord> implements WarningRecordService {

    @Resource
    private WarningRecordMapper warningRecordMapper;

    @Override
    public List<WarningVo> queryWarningSum(Page page, WarningVo warningVo,Integer adminId) {
        System.out.println(warningVo.toString());
        System.out.println(page.getSize());
        List<Warning> warningList = warningRecordMapper.queryWarningSum(new Warning(warningVo), (int) ((page.getCurrent() - 1) * page.getSize()), (int) (page.getSize()),adminId);
        List<WarningVo> warningVoList = new ArrayList();
        warningList.forEach((v)->{
            System.out.println(v.toString());
            warningVoList.add(new WarningVo(v));
        });
        return warningVoList;
    }

    @Override
    public Integer queryWarningCount(WarningVo warningVo,Integer adminId) {
        return warningRecordMapper.queryWarningCount(new Warning(warningVo),adminId);
    }

//    @Override
//    public WarningDeliveryVo queryDevice(Integer wrecId) {
//        return new WarningDeliveryVo(warningRecordMapper.queryWarningDelivery(wrecId));
//    }

    @Override
    public Boolean deleteWarning(ArrayList warningIdList) {
        int row = warningRecordMapper.deleteWarning(warningIdList);
        if (row == warningIdList.size()){
            return true;
        }
        //手动事务回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return false;
    }

    @Override
    public WarningDeliveryVo queryWarningDelivery(Integer wrecId) {
        return new WarningDeliveryVo(warningRecordMapper.queryWarningDelivery(wrecId));
    }

    @Override
    public boolean warningDelivery(Integer wrecId, Integer adminId) {
        if (1 == warningRecordMapper.warningDelivery(wrecId, adminId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean addWarningDelivery(Integer dinfId, WarningDeliveryVo warningDeliveryVo) {
        if (2 == warningRecordMapper.addWarningDelivery(dinfId, new WarningDelivery(warningDeliveryVo))){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateWarningDelivery(WarningDeliveryVo warningDeliveryVo) {
        if (2 == warningRecordMapper.updateWarningDelivery(new WarningDelivery(warningDeliveryVo))){
            return true;
        }
        return false;
    }
}
